package leetcode.dp;

public class UniquePaths62 {
    class Solution1 {
        public int uniquePaths(int m, int n) {
            if (m <= 0 || n <= 0) {
                return 0;
            }
            int[][] dp = new int[m][n];
            dp[0][0] = 1;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (i != 0 && j != 0) {
                        dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
                    } else if (i != 0) {
                        dp[i][j] = dp[i - 1][j];
                    } else if (j != 0) {
                        dp[i][j] = dp[i][j - 1];
                    }
                }
            }
            return dp[m - 1][n - 1];
        }
    }

    class Solution2 {
        public int uniquePaths(int m, int n) {
            if (m <= 0 || n <= 0) {
                return 0;
            }
            int[] dp = new int[n];
            dp[0] = 1;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (i != 0 && j != 0) {
                        dp[j] = dp[j] + dp[j - 1];
                    } else if (j != 0) {
                        dp[j] = dp[j - 1];
                    }
                }
            }
            return dp[n - 1];
        }
    }

    class Solution3 {
        public int uniquePaths(int m, int n) {
            if (m <= 0 || n <= 0) {
                return 0;
            }
            int[][] dp = new int[m + 1][n + 1];
            dp[0][1] = 1;
            for (int i = 1; i <= m; i++) {
                for (int j = 1; j <= n; j++) {
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
                }
            }
            return dp[m][n];
        }
    }

    class Solution4 {
        public int uniquePaths(int m, int n) {
            if (m <= 0 || n <= 0) {
                return 0;
            }
            int[] dp = new int[n + 1];
            dp[1] = 1;
            for (int i = 1; i <= m; i++) {
                for (int j = 1; j <= n; j++) {
                    dp[j] = dp[j] + dp[j - 1];
                }
            }
            return dp[n];
        }
    }

    //递归解法超时 Σ(°△°|||)︴
    class Solution5 {
        public int uniquePaths(int m, int n) {
            if (m == 1 || n == 1) {
                return 1;
            }
            return uniquePaths(m - 1, n) + uniquePaths(m, n - 1);
        }
    }
}
